package com.lanou.myim.model;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.util.HanziToPinyin;
import com.lanou.myim.db.ConversationEty;
import com.lanou.myim.db.ConversationEtyDao;
import com.lanou.myim.entity.MsgEty;
import com.lanou.myim.presenter.OnCompletedListener;
import com.lanou.myim.tools.DaoSingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/4.
 */
public class ConversationModel implements ConversationModelInterface {
    private ConversationEtyDao conversationEtyDao = DaoSingleton.getInstance().getConversationEtyDao();

    @Override
    public void insertMsg(MsgEty msgEty,final OnCompletedListener completedListener) {
        List<ConversationEty> conversationEties = conversationEtyDao.queryBuilder().list();
        if (null != conversationEties){
            conversationEtyDao.deleteAll();
            conversationEtyDao.insert(new ConversationEty(conversationEtyDao.count()
                    ,msgEty.getAccount(),msgEty.getUserName(),msgEty.getBody(),msgEty.getTime()));
            for (ConversationEty conversationEty:conversationEties){
                if (!conversationEty.getUserName().equals(msgEty.getUserName())){
                    conversationEtyDao.insert(new ConversationEty(conversationEtyDao.count()
                            ,conversationEty.getAccount(),conversationEty.getUserName(),conversationEty.getBody(),conversationEty.getTime()));
                }
            }
        }else {
            conversationEtyDao.insert(new ConversationEty(conversationEtyDao.count()
                    ,msgEty.getAccount(),msgEty.getUserName(),msgEty.getBody(),msgEty.getTime()));
        }
        completedListener.onCompleted();
    }


    @Override
    public List<MsgEty> getNormalMsgEtyList() {
        List<ConversationEty> conversationEtyList = conversationEtyDao.queryBuilder().list();
        List<MsgEty> msgEtyList = new ArrayList<>();
        for (ConversationEty conversationEty : conversationEtyList){
            if (conversationEty.getAccount().equals(EMChatManager.getInstance().getCurrentUser())){
                msgEtyList.add(new MsgEty(conversationEty.getAccount()
                        ,conversationEty.getUserName(),conversationEty.getBody(),conversationEty.getTime(),getUnreadMsgCount(conversationEty.getUserName())));
            }
        }
        return msgEtyList;
    }

    @Override
    public void deleteMsg(MsgEty msgEty,final OnCompletedListener completedListener) {
        List<ConversationEty> conversationEties = conversationEtyDao.queryBuilder().list();
        if (null != conversationEties) {
            conversationEtyDao.deleteAll();
            for (ConversationEty conversationEty : conversationEties) {
                if (!(conversationEty.getAccount().equals(msgEty.getAccount())
                        && conversationEty.getUserName().equals(msgEty.getUserName()))) {
                    conversationEtyDao.insert(new ConversationEty(conversationEtyDao.count(),conversationEty.getAccount()
                            ,conversationEty.getUserName(),conversationEty.getBody(),conversationEty.getTime()));
                }
            }
            completedListener.onCompleted();
        }
    }

    @Override
    public List<MsgEty> getStickiedMsgEtyList(String userName) {
        MsgEty stickiedMsg = null;
        List<ConversationEty> conversationEtyList = conversationEtyDao.queryBuilder().list();
        List<MsgEty> msgEtyList = new ArrayList<>();
        for (ConversationEty conversationEty : conversationEtyList){
            if (conversationEty.getAccount().equals(EMChatManager.getInstance().getCurrentUser())
                    &&conversationEty.getUserName().equals(userName)){
                 stickiedMsg = new MsgEty(conversationEty.getAccount()
                         ,conversationEty.getUserName(),conversationEty.getBody(),conversationEty.getTime(),getUnreadMsgCount(conversationEty.getUserName()));
            }else if (conversationEty.getAccount().equals(EMChatManager.getInstance().getCurrentUser())){
                msgEtyList.add(new MsgEty(conversationEty.getAccount()
                        ,conversationEty.getUserName(),conversationEty.getBody(),conversationEty.getTime(),getUnreadMsgCount(conversationEty.getUserName())));
            }
        }
        if (stickiedMsg != null){
            msgEtyList.add(0,stickiedMsg);
        }
        return msgEtyList;
    }

    @Override
    public Integer getUnreadMsgCount(String userName) {
        EMConversation conversation = EMChatManager.getInstance().getConversation(userName);
        return conversation.getUnreadMsgCount();
    }

    @Override
    public String getPinYin(String input) {
        ArrayList<HanziToPinyin.Token> tokens = HanziToPinyin.getInstance().get(input);
        StringBuilder sb = new StringBuilder();
        if (tokens != null && tokens.size() > 0) {
            for (HanziToPinyin.Token token : tokens) {
                if (token.type == HanziToPinyin.Token.PINYIN) {
                    sb.append(token.target);
                } else {
                    sb.append(token.source);
                }
            }
        }
        return sb.toString().toLowerCase();
    }

    @Override
    public List<MsgEty> searchMsg(String str) {
        List<MsgEty> msgEties = getNormalMsgEtyList();
        List<MsgEty> filter = new ArrayList<>();
        int length = str.length();
        if (length == 0){
            return msgEties;
        }else {
            for (int i = 0; i <msgEties.size() ; i++) {
                String pinYin = getPinYin(msgEties.get(i).getUserName());
                int limitLength = pinYin.length();
                if (length<=limitLength && pinYin.substring(0,length).equals(str)){
                    filter.add(msgEties.get(i));
                }
            }
            return filter;
        }
    }

}
